﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.AdminCP.Common;
using LinkCard.Dao;
using LinkCard.Model;
//using LinkCard.Utils;


namespace LinkCard.AdminCP.Reports
{
    public partial class SystemLog : System.Web.UI.Page
    {
        public string sFromDate = "";
        public string sToDate = "";
        public string sAction = "";
        public DateTime dFromDate = new DateTime(1900, 1, 1);
        public DateTime dToDate = new DateTime(2099, 1, 1);

        private int _totalRecords;

        private int nPageSize = 40;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
                {
                    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "REPORT") == false)
                    {
                        Response.Redirect("/message_box.aspx?why=1&msg=5");
                    }
                }
            }

            if (!IsPostBack)
                LoadLogs();
        }
        private void LoadLogs()
        {
            var db = new TopLinkEntities();

            //Int64 nSupplierID = getSupplierID();
            int curpage = 0;
            int skip = 0;

            if (Request.QueryString["page"] != null)
            {
                if (Request.QueryString["page"].ToString() != "1")
                {
                    curpage = Convert.ToInt32(Request.QueryString["page"]);
                    skip = (curpage - 1) * nPageSize;
                }
                else
                {
                    curpage = 1;
                    skip = 0;
                }
            }
            else
            {
                curpage = 1;
                skip = 0;
            }

            System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
            dateInfo.ShortDatePattern = "dd/MM/yyyy";

            if (sFromDate != "")
            {
                sFromDate += " 00:00:00";
                dFromDate = Convert.ToDateTime(sFromDate, dateInfo);

            }
            if (sToDate != "")
            {
                sToDate += " 23:59:59";
                dToDate = Convert.ToDateTime(sToDate, dateInfo);
            }

            //List<Logs> paylog = new List<Logs>();
            var _action = "";

            if (action.Value == "0")
            {
                _action = "";
                //paylog = db.Logs.Where(t => t.UserID != 0
                //                            && t.DateLog >= dFromDate
                //                            && t.DateLog <= dToDate)
                //                                                    .OrderByDescending(t => t.DateLog).Skip(skip).Take(nPageSize).ToList();
                
                //var paylog = (from _log in db.Logs
                //          from _user in db.Users
                //          where (_log.UserID == _user.UserID
                //                && _log.DateLog >= dFromDate
                //                && _log.DateLog <= dToDate)
                //          select new
                //          {
                //              _log.DateLog,
                //              _log.Action,
                //              _log.Category,
                //              _log.Info,
                //              _log.SupplierID,
                //              _log.PayID,
                //              _log.CustomerID,
                //              _log.ShopStaffID,
                //              _user.Username
                //          }).OrderByDescending(t => t.DateLog).Skip(skip).Take(nPageSize).ToList();

                //_totalRecords = db.Logs.Where(t => t.UserID != 0
                //                            && t.DateLog >= dFromDate
                //                            && t.DateLog <= dToDate)
                //                                                    .OrderByDescending(t => t.DateLog).ToList().Count;
            }
            else
            {
                _action = action.Value;
            }

            var paylog = (from _log in db.Logs
                      from _user in db.Users
                      where (_log.UserID == _user.UserID
                            && _log.DateLog >= dFromDate
                            && _log.DateLog <= dToDate
                            && _log.Action.Contains(_action))
                      select new
                      {
                          _log.DateLog,
                          _log.Action,
                          _log.Category,
                          _log.Info,
                          _log.SupplierID,
                          _log.PayID,
                          _log.CustomerID,
                          _log.ShopStaffID,
                          _user.Username
                      }).OrderByDescending(t => t.DateLog).Skip(skip).Take(nPageSize).ToList();

            _totalRecords = db.Logs.Where(t => t.UserID != 0
                                     && t.DateLog >= dFromDate
                                     && t.DateLog <= dToDate
                                     && t.Action.Contains(_action))
                                                            .OrderByDescending(t => t.DateLog).ToList().Count;
            if (paylog.Count > 0)
            {
                //_totalRecords = paylog.Count;

                int nPageCount;

                if (_totalRecords % nPageSize == 0)
                {
                    nPageCount = _totalRecords / nPageSize;
                }
                else
                {
                    nPageCount = Convert.ToInt32(_totalRecords / nPageSize) + 1;
                }

                if (nPageCount < 2) divPager.Visible = false;

                PagerShow.Text = LinkCard.Utils.hash1way.ShowPageNavigation(curpage, nPageCount);

                msgList.DataSource = paylog;

                msgList.DataBind();
                msgNoData.Visible = false;
            }
            else
            {

                msgList.DataSource = null;
                msgList.DataSourceID = null;
                msgList.DataBind();
                msgNoData.Visible = true;
            }

        }
       

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            sFromDate = FromDate.Value.Trim();
            sToDate = ToDate.Value.Trim();
            sAction = this.action.Value.Trim();
            LoadLogs();
        }
    }
}